<?php
namespace api\modules\v1\controllers\trade;
use Yii;
use api\modules\v1\controllers\CoreController;
use api\modules\models\member\Member;
use api\modules\models\finance\Balance;
use common\models\finance\ExchangeLogModel;
use common\service\ExchangeService;
/*
	账本兑换
*/

class ExchangeController extends CoreController
{
	
	/*
		* 兑换配置信息
		* b_type 账本类型
	*/
	public function actionExchange_conf()
	{
		if(!$btype = $this->request('b_type')) $this->error('账本类型有误');
		$res = ExchangeService::exchangeConf(['b_type'=>$btype,'member_id'=>$this->_memberId]);
		if(!$res['state']) $this->error($res['msg']);
		$this->out('兑换配置信息',$res['data']);
	}



	/*
		兑换
		* btype1
		* btype2  btype1 兑换成 btype2
		* money 
		* password 密码
	*/
	public function actionBalance_exchange()
	{
		if(md5($this->request('password'))!=$this->_member['paypwd']) $this->error('密码错误');
		if(!$this->_member['state']) return ['state'=>false,'msg'=>'你暂不能此操作'];
		$params = $this->request;
		$params['member_id'] = $this->_memberId;
		$res = ExchangeService::exchange($params);
		if($res['state']) $this->out('操作成功');
		$this->error($res['msg']);
	}


	/*
		兑换记录
		* page 
		* page_size
		* b_type 选填 默认全部
	*/
	public function actionExchange_log()
	{
		$whereArr = array();
		$whereArr['where'] = ['member_id'=>$this->_memberId];
		if($this->request('b_type')) $whereArr['where']['b_type1'] = $this->request('b_type');
		$params = array(
			'field' => ['*'],
			'page'	=> $this->request('page',1),
			'limit' => $this->request('page_size',10),
			'order' => 'id DESC'
		);		
		$data = ExchangeLogModel::exchangeList($whereArr,$params);
		$typeNames = Balance::balanceTypeName();
		foreach($data as &$val)
		{
			$val['describe'] = $typeNames[$val['btype1']] .' to '. $typeNames[$val['btype2']];
		}

		$this->out('兑换列表',$data);
	}


}